﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Reflection;
using XWG.JWBST.JWBSTSPConst;
using Newtonsoft.Json;
using System.Text.RegularExpressions;
using System.Data;
using XWG.Framework.AuthUtility;
using XWG.Framework.AuthSPConst;
using XWG.Framework.IAuthority;
using System.Collections;

namespace JWBSTAPP.CommonClass
{
    /// <summary>
    /// 所有HttpHandler处理类的基类 如果类中需实现权限和页面的基本功能 请继承此类
    /// </summary>
    public class HttpHandlerBase : IHttpHandler
    {
        /// <summary>
        /// 实现基类接口Request响应。（如果子类自定义ProcessRequest，子类需同时继承IHttpHandler来覆盖此方法）
        /// </summary>
        /// <param name="context"></param>
        public void ProcessRequest(HttpContext context)
        {
            ResultModel mResultModel;
            BindingFlags bf = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public;
            MethodInfo[] m = this.GetType().GetMethods(bf);
            string action = context.Request.Params["action"];
            if (String.IsNullOrEmpty(action))
            {
                return;
            }
            MethodInfo mi = this.GetType().GetMethod("");
            if (this.GetType().GetMethod(ExMethodJWBST.Authority.Instance().GetMethodName(action), bf) != null)
            {
                mi = this.GetType().GetMethod(ExMethodJWBST.Authority.Instance().GetMethodName(action), bf);

            }
            else if (this.GetType().GetMethod(action, bf) != null)
            {
                mi = this.GetType().GetMethod(action, bf);
            }
            if (mi != null)
            {
                PermissionMethodAttribute att = Attribute.GetCustomAttribute(mi, typeof(PermissionMethodAttribute)) as PermissionMethodAttribute;

                if (att != null)
                {
                    if (att.Check())
                          mi.Invoke(this, null);  
                    else
                    {
                        mResultModel = new ResultModel();
                        mResultModel.ISSucceed = false;
                        mResultModel.ErrorCode = -9989;
                        HttpContext.Current.Response.Write(JsonConvert.SerializeObject(mResultModel));
                    }
                }
                else
                {
                    mi.Invoke(this, null);
                }
            }

        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
        #region 下拉框JSON序列化类
        /// <summary>
        ///  绑定下拉框
        /// </summary>
        public class ControlID
        {
            public string name { get; set; }//名称
            public string value { get; set; }//值
        }
        #endregion
        #region 反序列化
        /// <summary>
        ///  反序列化JSON成查询条件
        /// </summary>
        public class JSONParam
        {
            public string column { get; set; }//查询列
            public string value { get; set; }//值
        }
        /// <summary>
        ///  反序列化JSON
        /// </summary>
        public class JSONData
        {
            public string id { get; set; }//主键
            public string[] created { get; set; }//写入序列
            public string[] deleted { get; set; }//删除序列
        }
        /// <summary>
        ///  反序列化JSON
        /// </summary>
        public class JSONDataTwo
        {
            public string userid { get; set; }//管理员
            public string id { get; set; }//主键
            public string[] created { get; set; }//写入序列
            public string[] deleted { get; set; }//删除序列
        }
     
        /// <summary>
        ///  反序列化JSONCreated
        /// </summary>
        public class JSONCreated
        {
            public string[] created { get; set; }
        }
        /// <summary>
        ///  反序列化JSONDeleted
        /// </summary>
        public class JSONDeleted
        {
            public string[] deleted { get; set; }
        }

        
#endregion
        #region 图形数据
        public class HigtChart
        {
             
                public string[] categories
                {
                    get;
                    set;
                }
            
            public  series  series
            {
                get;
                set;
            }
            
        }
        public class series
        {
            public string name
            {
                get;
                set;

            }
            public string[] data
            {
                get;
                set;
            }
        }
        #endregion
        #region Flexigrid JSON序列化类
        /// <summary>
        ///  自定义列表行集
        /// </summary>
        public class FlexigridRow
        {
            public string id { get; set; }
            public bool @checked { get; set; }//是否选中
            public string[] cell { get; set; }//行集
        }
        /// <summary>
        ///  自定义列表
        /// </summary>
        public class FlexigridObject
        {
            public int page;//页数
            public int total;//总记录数
            public List<FlexigridRow> rows = new List<FlexigridRow>();
            public List<col> colModel = new List<col>();
        }
        /// <summary>
        ///  自定义列表表头
        /// </summary>
        public class col
        {
            //listbox实例
            public string display { get; set; }//显示列
            public string name { get; set; }//显示列
            public int width { get; set; }//列宽
            public bool sortable { get; set; }//是否排序
            public string align { get; set; }//align方式
            public bool hide { get; set; }//是否显示
            public bool iskey { get; set; }
        }
        #endregion
        #region ztree 序列化类
        /// <summary>
        ///  树形菜单类
        /// </summary>
        public class TreeNode
        {
            public string id { get; set; }//唯一标示
            public string pId { get; set; }//父节点
            public string name { get; set; }//名字
            public string url { get; set; }//连接
            public string target { get; set; }//target类型
            public string click { get; set; }//单击事件
            public bool open { get; set; }//是否默认展开
            public bool @checked { get; set; }//是否默认展开
            public string icon { get; set; }//默认图标
            public string iconOpen { get; set; }//图标打开状态图标
            public string iconClose { get; set; }//图标关闭状态图标

        }
        #endregion
        #region 前台页面表单 JSON 序列化数据
        public class TableMessage
        {
            public string totalCount { get; set; }//总页数
            public object data { get; set; }//数据
            public List<HearderMessage> Header { get; set; }//表头
        }
        public class HearderMessage
        {
            public string name { get; set; }//表头
            public string width { get; set; }//宽度
        }

        /// <summary>
        /// 数据模型
        /// </summary>
        public class SJMX
        {
            public ArrayList SortCoulmns
            {
                get;
                set;
            }
            public ArrayList SortType
            {
                get;
                set;
            }
            public ArrayList WhereColumns
            {
                get;
                set;
            }
            public ArrayList WherePoerat
            {
                get;
                set;
            }
            public ArrayList WhereValue
            {
                get;
                set;

            }
            public ArrayList WhereType
            {
                get;
                set;
            }
            public ArrayList JoinColumn1
            {
                get;
                set;

            }
            public ArrayList JoinColumn2
            {
                get;
                set;
            }
            public ArrayList JoinType
            {
                get;
                set;
            }
            public ArrayList GroupColumns
            {
                get;
                set;
            }
            public ArrayList HavingColumns
            {
                get;
                set;
            }
            public ArrayList HavingOperat
            {
                get;
                set;
            }
            public ArrayList HavingValue
            {
                get;
                set;
            }
            public ArrayList HavingType
            {
                get;
                set;
            }
            public ArrayList SelectedItems
            {
                get;
                set;
            }
        }
        #endregion
        #region 错误处理函数
        public string Message(string result)
        {
            if (result == "1")
            {
                return "操作成功";
            }
            else
            {
                if (IsGUID(result))
                    return "操作成功";
                else
                    return XWG.Framework.AuthSPConst.ExCode.GetMessage(result);
            }
        }
        /// <summary>
        ///  处理调用方法后的结果
        /// </summary>
        /// <param name="result">需要处理的信息代码</param>
        public bool Succeed(string result)
        {
            if (result == "1")
            {
                return true;
            }
            else
            {
                try
                {
                    if (Convert.ToInt32(result)<0)
                        return false;
                    else
                        return true;
                }
                catch (Exception ex)
                { 
                
                }
            }
            return true;
        }
        public bool IsGUID(string str)
        {
            Match m = Regex.Match(str, @"^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$", RegexOptions.IgnoreCase);
            if (m.Success)
            {
                //可以转换 
                //Guid guid = new Guid(str);
                return true;
            }
            else
            {
                //不可转换 
                return false;
            }
        }
        #endregion
        
    }
}